package br.estacio.contatos.client.ui;

import java.util.List;

import br.estacio.contatos.client.i18n.ContatosConstants;
import br.estacio.contatos.shared.Contato;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Widget;

public class ListarContatoViewImpl extends ViewInternacionalizada implements ListarContatoView {

	private static ListarContatoViewImplUiBinder uiBinder = GWT.create(ListarContatoViewImplUiBinder.class);

	interface ListarContatoViewImplUiBinder extends
			UiBinder<Widget, ListarContatoViewImpl> {
	}
	
	@UiField
	CellTable<Contato> contatosCellTable;

	public ListarContatoViewImpl(ContatosConstants contatosConstants) {
		super(contatosConstants);
		initWidget(uiBinder.createAndBindUi(this));
		configurarColunasParaTabela();
	}

	private void configurarColunasParaTabela() {
		Column<Contato, String> nomeColumn = new TextColumn<Contato>() {
			@Override
			public String getValue(Contato c) {
				return c.getNome();
			}
		};
		
		Column<Contato, String> emailColumn = new TextColumn<Contato>() {
			@Override
			public String getValue(Contato c) {
				return c.getEmail();
			}
		};
		
		Column<Contato, String> telefoneColumn = new TextColumn<Contato>() {
			@Override
			public String getValue(Contato c) {
				return c.getTelefone();
			}
		};
		
		nomeColumn.setSortable(true);
		emailColumn.setSortable(true);
		telefoneColumn.setSortable(true);
		
		contatosCellTable.addColumn(nomeColumn, getContatosConstants().nome());
		contatosCellTable.addColumn(emailColumn, getContatosConstants().email());
		contatosCellTable.addColumn(telefoneColumn, getContatosConstants().telefone());
		
	}

	@Override
	public void setListagemContatos(List<Contato> contatos) {
		contatosCellTable.setRowData(contatos);
	}

}
